/**
 * Utilities
 */
function ComaUtil() {

    /**
     * Do a pad left of text / number.
     */
    this.padL = function (a, b, c) { //string/number,length=2,char=0
        return (new Array(b || 2).join(c || 0) + a).slice(-b)
    }
    /**
     * Converts a dd/mm/aaaa date format into aaaa-mm-dd
     */
    this.DateToDB = function (TextDate) {

        if (TextDate < 10)
            return null;
        var array = TextDate.split('/');

        var d = new Date(Date.UTC(array[2], array[1], array[0], 0, 0, 0, 0));

        var mysqldate = d.getUTCFullYear() + '-' + this.padL(d.getUTCMonth(), 2) + '-' + this.padL(d.getUTCDate(), 2) + ' ' + this.padL(d.getUTCHours(), 2) + ':' + this.padL(d.getUTCMinutes(), 2) + ':' + this.padL(d.getUTCSeconds(), 2);

        comaLog('Raw date: ' + d.toString());
        comaLog('Converting ' + TextDate + ' to ' + mysqldate);
        return mysqldate;
    }

    /**
     * Given a 2019-08-07 22:00:00 data, returns dd/mm/aaaa
     */
    this.DBtoDateString = function (DbDate) {
        if (DbDate < 10)
            return null;
        var dateParts = DbDate.split("-");
        var d = new Date(dateParts[0], dateParts[1] - 1, parseInt(dateParts[2].substr(0, 2)) + 1, dateParts[2].substr(3, 2), dateParts[2].substr(6, 2), dateParts[2].substr(9, 2));

        return d.getUTCDate() + '/' + (d.getUTCMonth() + 1).toString() + '/' + d.getUTCFullYear();
    }

    /**
     * Given a javascript Date returns a DD/MM/AAAA string
     */
    this.DateToString = function (d) {
        return d.getUTCDate() + '/' + (d.getUTCMonth() + 1).toString() + '/' + d.getUTCFullYear();
    }
}
